How to Send Email using PHPMailer in Laravel

Send Email using PHPMailer in Laravel

By default, Laravel sends emails using the SwiftMailer library. SwiftMailer is the inbuilt library in Laravel. Mostly, we used the default mail() function to send emails in the PHP-based web application. Now, here we will try “PHPMailer” Laravel package for sending emails. PHPMailer is an open-source package and easy to set up and use in the laravel application. This package includes many features, like the ability to send multiple emails with To, CC, BCC, and Reply-to-addresses. It protects against header injection attacks in terms of security. That’s why, it is extremely safe to use. You can include attachments in your emails, including inline attachments.

I’ll show you how to Send Emails using PHPMailer in Laravel. Most of the developers always recommended using an SMTP server to send an email from a script in a web application. We already posted the article on the PHPMailer library to send email with an SMTP server using PHP.

 

Send Email using PHPMailer in Laravel

Here, for installing PHPMailer package in Laravel Application, I will be using the composer. You can use the Laravel installer too. Open the command prompt and hit the below command.

Configure PHPMailer in Laravel

After installing the package, you can check the composer.json file to confirm that PHPMailer package installed or not. You will find composer.json file in the root of the project directory.

package.json

Configure PHPMailer in Laravel For Sending Email

Let’s move to the next step, Now we will create a controller for sending emails. This controller file will contain the PHPMailer Script and will configure the SMTP setting.

Create PHPMailerController.php file

To create a controller file with the name PHPMailerController.php, Follow the below command.

Use PHPMailer and Exceptional classes

Now at very first, Load the PHPMailer and Exception classes to configure the PHPMailer SMTP.

Open the PHPMailerController.php file and simply paste the below code snippet.

In the above code, You don’t forget to update the sender details like as username, password, sender email, sender name to send an email using your own credentials.

After placing the above snippets, you will require to create routes in the web.php file.

Create Routes For PHPMailer

Now, Add the below code to create routes into the web.php file. There are two routes defined here. The first route (Using GET Method) is used to load the view from where we will send the email. The second route (Using POST Method) is to compose an email.

Create a View For Sending Email Using PHPMailer

Let’s create a view file with the name email.blade.php in the resources/views directory for sending a successful email. Now, you have to copy and paste the below code snippet for a basic HTML form design.

Let’s, save the file email.blade.php. The above snippet will generate a form as showing below.

Send Email using PHPMailer in Laravel

Here, in the above form, We have entered the email recipient, cc, bcc, subject, email body, and attachment.

After this, submit the send Email button. In the response, you will get a success message that email has been sent.

If you will use Gmail SMTP to send an email, you must first create an app password for your Gmail account and enable less secure app access.

Send email using Gmail SMTP server in Laravel

To use Gmail SMTP with the PHPMailer library in Laravel, you must be changed some settings in the Google account. Please follow the below steps.

  • Step-1: Login to your Google account. Go to the My Account page
  • Step-2: Navigate to the Security page and scroll down to the Signing into Google section » Turn Off the 2-Step Verification
  • Step-3: Scroll down the Less secure app access section and turn On Less secure app access.

Hey, You are done to use Gmail SMTP to send emails from the Laravel application.

Update your Gmail account credentials (email address and password) in the below code snippet

Now you are ready to send email using Gmail SMTP in Laravel.

good luck

Conclusion

Well, In this article you will get the complete steps to Send an Email with an attachment using PHPMailer in Laravel. You can extend the functionality as per your requirement. I hope you found this tutorial helpful for your project. Keep learning!.

Having trouble? Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request

Related posts